﻿var key = '';
var UP = 38;
var DOWN = 40;
var ENTER = 13;
var TAB = 9;
var BS = 8;
var DASH = '-';
function GetKeyword(e) {

    var num
    var char
    if (window.event) // IE
    {
        num = e.keyCode
        num = e.charCode ? e.charCode : e.keyCode
    }
    else if (e.which) // Netscape/Firefox/Opera
    {
        num = e.which
    }
    char = String.fromCharCode(num)
    
    if (num != DOWN && num != UP && (num >= 65 && num <= 90)) {
        key = key + char;
    }
    else if (num == 109 || num == 189) {
        key = key + DASH;
    }
    else if (num == BS) {
        key = key.substring(0, key.length - 1);
    }
    else {
        return false;
    }
    Clear();
    if (key.length > 0) {
        GetCommands(key);
    }
}
function Clear() {
    $("#cmds").empty();
    $("#results").empty();
    $("#examples").empty();

}
function ClearKey() {
    $('#keyword').val('');
    key = '';
    Clear();
}
function GetExamples(cmd) {
    $("#examples").empty();
    $.ajax({
        url: "Home/ReturnExamples",
        data: "strCommand=" + cmd,
        dataType: "json",
        type: "POST",
        success: function (jsondata) {
            if (jsondata.length > 0) {
                var data = eval(jsondata[0]);
                var examples = data.Examples;
                $("#examples").append("<b>Name : </b><br/>" + data.Name + "<br/><br/>");
                $("#examples").append("<b>Synopsis : </b><br/>" + data.Synopsis + "<br/><br/>");
                $("#examples").append("<b>Description : </b><br/>" + data.Description + "<br/><br/>");
                $("#examples").append("<b>Syntax : </b><br/>" + data.Syntax + "<br/><br/>");
                $("#examples").append("<b>Examples : </b><br/><br/>");
                if (examples != 'undefined' && examples.length > 0) {
                    $.each(examples, function (index) {
                        $("#examples").append("<b>" + examples[index].Title + "</b><br/>" + examples[index].Code + "<br/>" + examples[index].Remarks + "<br/><br/>");
                    });
                }
            }
        }
    });
}
function GetCommands(key) {
    $.ajax({
        url: "Home/ReturnCommands",
        data: "keyword=" + key,
        dataType: "json",
        type: "POST",
        success: function (jsondata) {
            if (jsondata.length > 0) {
                $("#results").empty();
                $('#cmds').empty();
                $("#results").append("<b> # of cmdlets : " + jsondata.length + "</b>");
                var rows = "";
                $.each(jsondata, function (index) {
                    rows += "<tr id='actionRow'><td><a href=javascript:GetExamples('" + jsondata[index].Name + "') title='" + jsondata[index].Synopsis + "'>" + jsondata[index].Name.replace(new RegExp(key, 'gi'), '<b>' + key.toUpperCase() + '</b>') + "</a></td></tr>";
                });
                rows = "<table id='pscmds' border=1 cellspacing=2 cellpadding=2><thead><th>Command Name</th></thead><tbody>" + rows + "</tbody></table>";
                $("#cmds").append(rows);
                $('#pscmds > tbody > tr:odd').css("background-color", "#F4F4F8");
                $('#pscmds > tbody > tr:even').css("background-color", "#ecf6fc");
            }
        }
    });
}
